package test2.experiment6;

public class Practice5_2 {
    public static void main(String[] args) {
        String[] as = {"a1", "a2", "a3", "a4"};
        int[] bs = {100, 100, 100, 100};
        Transfer tf = new Transfer(as, bs);
        try {
            tf.move("a1", "a2", 150);
        } catch (TransferException e) {
            System.out.println("Exception occurs: " + e.getMessage());
        }
        System.out.println("a1 " + bs[0]);
        System.out.println("a2 " + bs[1]);
    }
}

class Transfer {
    String[] accs;
    int[] bals;

    public Transfer(String[] accs, int[] bals) {
        this.accs = accs;
        this.bals = bals;
    }

    public void move(String from, String to, int total) throws TransferException {
        int i;
        boolean f = false, t = false;
        int indf = 0, indt = 0;

        for (i = 0; i < accs.length; i++) {
            if (f == false) {
                f = accs[i].equals(from);
                indf = i;
            }
            if (t == false) {
                t = accs[i].equals(to);
                indt = i;
            }
            if (f == true && t == true) break;
        }

        if (f == false) {
            throw new TransferException("No such account " + from);
        }
        if (t == false) {
            throw new TransferException("No such account " + from);
        }
        bals[indf] -= total;
        if (bals[indf] < 0) {
            throw new TransferException(from + " is insufficient");
        }
        bals[indt] += total;
        return;
    }
}

class TransferException extends Exception {
    public TransferException(String string) {
        super(string);
    }
}